<?php
$idSession = session_id();
if (empty($idSession)) {
    session_start();
}

require_once("../../util/conexao.php");

function retornaPermissaoMenu() {

    $conexao = Conexao::conectar();
    $permissao = array();

    $query = "select idMenu from tbusuarioxmenu where idUsuario='" . $_SESSION["dadosUsuario"]["id"] . "' and deletado='N'";
    $res = mysql_query($query, $conexao);

    while ($row = mysql_fetch_object($res)) {
        $permissao[] = $row->idMenu;
    }

    Conexao::desconectar();

    return $permissao;
}

function retornarMenu($categoria) {

    $conexao = Conexao::conectar();
    $menus = array();

    $query = "select * from tbmenu where categoria='" . $categoria . "' and deletado='N'";
    $res = mysql_query($query, $conexao);

    $i = 0;
    while ($row = mysql_fetch_object($res)) {
        $menus[$i]["id"] = $row->id;
        $menus[$i]["menu"] = $row->menu;
        $menus[$i]["link"] = $row->link;
        $i++;
    }

    Conexao::desconectar();

    return $menus;
}

function retornaQtdCarrosOficina() {

    $conexao = Conexao::conectar();

    $query = "select count(distinct(idVeiculo)) as total from tbveiculooficina where dataSaida is NULL and deletado='N' group by idVeiculo";
    $res = mysql_query($query, $conexao);
    $num = mysql_num_rows($res);

    if ($num > 0) {
        $total = 0;
        while ($row = mysql_fetch_object($res)) {
            $total++;
        }
    } else {
        $total = 0;
    }

    Conexao::desconectar();

    return $total;
}

function retornaQtdVeiculosCadastrados() {

    $conexao = Conexao::conectar();

    $query = "select count(*) as total from tbveiculo where estoqueLoja='S' and deletado='N'";
    $res = mysql_query($query);
    $row = mysql_fetch_object($res);

    return $row->total;

    Conexao::desconectar();
}

$menu_cadastro = retornarMenu("CADASTRO");
$menu_financeiro = retornarMenu("FINANCEIRO");
$menu_veiculo = retornarMenu("VEICULO");
$menu_documento = retornarMenu("DOCUMENTO");
$menu_relatorios = retornarMenu("RELATORIO");
$menu_configuracao = retornarMenu("CONFIGURACAO");

$total_veiculos_oficina = retornaQtdCarrosOficina();
$total_veiculos = retornaQtdVeiculosCadastrados();

$permissao = retornaPermissaoMenu();
?>
<dl>
    <dt class='menu-cabecalho'>Cadastros</dt>
    <?php
    foreach ($menu_cadastro as $cadastro) {
        if (in_array($cadastro["id"], $permissao)) {
            ?>
            <dd><a href="<?= $url ?>/modulo/<?= $cadastro["link"] ?>"><?= utf8_encode($cadastro["menu"]) ?></a></dd>
            <?php
        }
    }
    ?>
</dl>
<dl>
    <dt class='menu-cabecalho'>Financeiro</dt>
    <?php
    foreach ($menu_financeiro as $financeiro) {
        if (in_array($financeiro["id"], $permissao)) {
            ?>
            <dd><a href="<?= $url ?>/modulo/<?= $financeiro["link"] ?>"><?= utf8_encode($financeiro["menu"]) ?></a></dd>
            <?php
        }
    }
    ?>
</dl>
<dl>
    <dt class='menu-cabecalho'>Veículos</dt>
    <?php
    foreach ($menu_veiculo as $veiculo) {
        if (in_array($veiculo["id"], $permissao)) {

            if ($veiculo["menu"] == "Controle de Oficina") {
                $total = "(<strong>" . $total_veiculos_oficina . "</strong>)";
            } elseif (utf8_encode($veiculo["menu"]) == "Compra de Veículos") {
                $total = "(<strong>" . $total_veiculos . "</strong>)";
            } else {
                $total = "";
            }
            ?>
            <dd><a href="<?= $url ?>/modulo/<?= $veiculo["link"] ?>"><?= utf8_encode($veiculo["menu"]) ?> <?= $total ?></a></dd>
            <?php
        }
    }
    ?>
</dl>
<dl>
    <dt class='menu-cabecalho'>Documentos</dt>
    <?php
    foreach ($menu_documento as $documento) {
        if (in_array($documento["id"], $permissao)) {
            ?>
            <dd><a href="<?= $url ?>/modulo/<?= $documento["link"] ?>"><?= utf8_encode($documento["menu"]) ?></a></dd>
            <?php
        }
    }
    ?>
</dl>
<dl>
    <dt class='menu-cabecalho'>Relatórios</dt>
    <?php
    foreach ($menu_relatorios as $relatorios) {
        if (in_array($relatorios["id"], $permissao)) {
            ?>
            <dd><a href="<?= $url ?>/modulo/<?= $relatorios["link"] ?>"><?= utf8_encode($relatorios["menu"]) ?></a></dd>
            <?php
        }
    }
    ?>
</dl>
<dl>
    <dt class='menu-cabecalho'>Configurações</dt>
    <?php
    foreach ($menu_configuracao as $configuracao) {
        if (in_array($configuracao["id"], $permissao)) {
            ?>
            <dd><a href="<?= $url ?>/modulo/<?= $configuracao["link"] ?>"><?= utf8_encode($configuracao["menu"]) ?></a></dd>
            <?php
        }
    }
    ?>
</dl>
